<?php
namespace app\common\service\shop;
use app\design\controller\Data as Rndata;
use think\facade\Db;
use think\facade\Config;

/**
 * 安装应用数据库
 */
class Data
{

    public function set_mode($data=[])
    {
        $app=Db::name("rn_curd_app")->where("rn_num='".$data['config']['set_mode']['rn_num']."'")->field("id")->find();
        $savedata['rn_num']=$data['config']['set_mode']['rn_num'];
        $savedata['rn_name']=$data['config']['set_mode']['rn_name'];
        $appdata=array_merge($this->moren_data(),$savedata);
        $c_app_num=$savedata['rn_num'];
        if (empty($app['id']))
        {
            $c_app_id=Db::name("rn_curd_app")->insertGetId($appdata);
        }
        else
        {
            Db::name("rn_curd_app")->where("id='".$app['id']."'")->update($appdata);
            $c_app_id=$app['id'];
        }
        $modearr=$data['config']['set_mode']['mode'];
        foreach($modearr as $key2 =>$rn2)
        {
            $savedata=$rn2;
            unset($savedata['set_sql']);
            unset($savedata['set_from']);
            unset($savedata['id']);
            $postdata=$rn2;
            $c_mode_num=$rn2['rn_num'];
            $modedata=array_merge($this->moren_data(),$savedata);
            $modedata['c_app_id']=$c_app_id;
            $mode=Db::name("rn_curd_mode")->where("rn_num='".$rn2['rn_num']."' and c_app_id='".$c_app_id."'")->field("id")->find();
            if (empty($mode['id']))
            {
                $c_mode_id=Db::name("rn_curd_mode")->insertGetId($modedata);
            }
            else
            {
                Db::name("rn_curd_mode")->where("id='".$mode['id']."'")->update($modedata);
                $c_mode_id=$mode['id'];
            }
            $postdata['c_app_id']=$c_app_id;
            $postdata['c_app_num']=$data['config']['set_mode']['rn_num'];
            $postdata['c_mode_id']=$c_mode_id;
            $postdata['c_mode_num']=$rn2['rn_num'];
            $this->set_sql($postdata);
            $this->set_from($postdata);
            $this->set_from_field($postdata);
            $this->set_from_table($postdata);
        }
    }

    public function set_config($data)
    {
        $config=$data['config']['set_config'];
        foreach($config as $key =>$rn) {
            $rn_curd_config=Db::name('rn_curd_config')->where("rn_del=0 and group_num='".$rn['group_num']."' and rn_num='".$rn['rn_num']."'")->field("id")->find();
            $appdata['group_num']=$rn['group_num'];
            $appdata['rn_num']=$rn['rn_num'];
            $appdata['rn_name']=$rn['rn_name'];
            if (empty($rn_curd_config['id']))
            {
                $config_id=Db::name("rn_curd_config")->insertGetId($appdata);
            }
            else
            {
                Db::name("rn_curd_config")->where("id='".$rn_curd_config['id']."'")->update($appdata);
                $config_id=$rn_curd_config['id'];
            }
            $setting=$rn['setting'];
            foreach($setting as $key2 =>$rn2)
            {
                $rn2['rn_mid']=$config_id;
                $config_list=Db::name('rn_curd_config_list')->where("rn_del=0 and rn_mid_num='".$rn['rn_num']."' and rn_num='".$rn2['rn_num']."'")->field("id")->find();
                if (empty($config_list['id']))
                {
                    Db::name("rn_curd_config_list")->insertGetId($rn2);
                }
                else
                {
                    Db::name("rn_curd_config_list")->where("id='".$config_list['id']."'")->update($rn2);
                }
            }
            $postdata=$rn;
            $postdata['source_id']=$config_id;
            $postdata['source_num']=$rn['rn_num'];
            $postdata['source_system']='config';
            $this->set_from($postdata,2);
            $this->set_from_field($postdata,2);

        }
    }
    public function set_menu($data)
    {
        $set_menu=$data['config']['set_menu'];
        $result=getSonsInfo($set_menu);
        $this->Install_menu($result,0);
    }
    public function Install_menu($arr,$rn_pid=0)
    {
        foreach($arr as $key2 =>$rn2)
        {
            $savedata=$rn2;
            $savedata['rn_pid']=$rn_pid;
            unset($savedata['id']);
            unset($savedata['child']);
            $modedata = array_merge($this->moren_data(), $savedata);
            $menu=Db::name('rn_admin_menu')->where("rn_num='".$rn2['rn_num']."'")->field("id")->find();
            if (empty($menu['id']))
            {
                $id=Db::name("rn_admin_menu")->insertGetId($modedata);
            }
            else
            {
                Db::name("rn_admin_menu")->where("id='".$menu['id']."'")->update($modedata);
                $id=$menu['id'];
            }
            if (!empty($rn2['child']))
            {
                $this->Install_menu($rn2['child'],$id);
            }
        }
    }






    public function set_sql($data)
    {
        $RnCurddata=new Rndata();
        $listdata[]=$data['set_sql'];
        foreach($listdata as $key2 =>$rn2) {
            $ppdata=array();
            $ppdata['RnData']['rn_num'] = $rn2['rn_num'];
            $ppdata['RnData']['rn_old_num'] = $rn2['rn_num'];
            $ppdata['RnData']['rn_name'] = $rn2['rn_name'];
            $table_field=$rn2['field'];
            $table_data=$rn2['data'];
            foreach($table_field as $key3 =>$rn3) {
                $ppdata['field'][$key3]['rn_field_sort'] = $rn3['rn_field_sort'];
                $ppdata['field'][$key3]['rn_field_num'] = $rn3['rn_field_num'];
                $ppdata['field'][$key3]['rn_field_old_num'] =$rn3['rn_field_num'];
                $ppdata['field'][$key3]['rn_field_name'] =$rn3['rn_field_name'];
                $ppdata['field'][$key3]['rn_field_type'] =$rn3['rn_field_type'];
                $ppdata['field'][$key3]['rn_field_len'] =$rn3['rn_field_len'];
                $ppdata['field'][$key3]['rn_field_moren'] =$rn3['rn_field_moren'];
                $ppdata['field'][$key3]['rn_field_yin'] =$rn3['rn_field_yin'];
            }
            $sql = "SHOW TABLES LIKE '".config::get('database.connections.mysql.prefix').$rn2['rn_num']."'";
            if (count(Db::query($sql))>0)
            {
                $RnCurddata->Rn_edit($ppdata['RnData'],$ppdata['field']);
            }
            else
            {
                $RnCurddata->Rn_add($ppdata['RnData'],$ppdata['field']);
            }
            //插入数据
            foreach($table_data as $key3 =>$rn3)
            {
                $table_num=Db::name($rn2['rn_num'])->where("id='".$rn3['id']."'")->field("id")->find();
                $table_data=$rn3;
                if (empty($table_num['id']))
                {
                    Db::name($rn2['rn_num'])->insert($table_data);
                }
                else
                {
                    Db::name($rn2['rn_num'])->where("id='".$table_num['id']."'")->update($table_data);
                }
            }
        }


    }

    public function set_from($data,$type=1)
    {

        $listdata[]=$data['set_from'];
        foreach($listdata as $key2 =>$rn2) {
            unset($rn2['field']);
            unset($rn2['table']);
            unset($rn2['id']);
            if ($type == 1) {
                $form = Db::name("rn_curd_from")->where("c_app_num='" . $data['c_app_num'] . "' and c_mode_num='" . $data['c_mode_num'] . "' and source_system='mode'")->field("id")->find();
                $modedata = array_merge($this->moren_data(), $rn2);
                $modedata['c_app_id'] = $data['c_app_id'];
                $modedata['c_mode_id'] = $data['c_mode_id'];
                $modedata['source_id'] = $data['c_mode_id'];
            } else {
                $form = Db::name("rn_curd_from")->where("source_num='" . $data['source_num'] . "' and source_system='" . $data['source_system'] . "'")->field("id")->find();
                $modedata = array_merge($this->moren_data(), $rn2);
                $modedata['source_num'] = $data['source_num'];
                $modedata['source_id'] = $data['source_id'];
                $modedata['source_system'] = $data['source_system'];
            }
            if (empty($form['id'])) {
                Db::name("rn_curd_from")->insertGetId($modedata);
            } else {
                Db::name("rn_curd_from")->where("id='" . $form['id'] . "'")->update($modedata);
            }
        }

    }


    public function set_from_field($data,$type=1)
    {
        $listdata=$data['set_from']['field'];
        foreach($listdata as $key2 =>$rn2) {
            unset($rn2['id']);
            if ($type == 1) {
                $form = Db::name("rn_curd_from_field")->where("c_app_num='" . $rn2['c_app_num'] . "' and c_mode_num='" . $rn2['c_mode_num'] . "' and fileds_form_num='" . $rn2['fileds_form_num'] . "' and source_system='mode'")->field("id")->find();
                $modedata = array_merge($this->moren_data(), $rn2);
                $modedata['c_app_id'] = $data['c_app_id'];
                $modedata['c_mode_id'] = $data['c_mode_id'];
                $modedata['source_id'] = $data['c_mode_id'];
            }
            else
            {
                $form = Db::name("rn_curd_from_field")->where("source_num='" . $data['source_num'] . "' and source_system='" . $data['source_system'] . "' and fileds_form_num='" . $rn2['fileds_form_num'] . "'")->field("id")->find();
                $modedata = array_merge($this->moren_data(), $rn2);
                $modedata['source_num'] = $data['source_num'];
                $modedata['source_id'] = $data['source_id'];
                $modedata['source_system'] = $data['source_system'];
            }
            if (empty($form['id']))
            {
                Db::name("rn_curd_from_field")->insertGetId($modedata);
            }
            else
            {
                Db::name("rn_curd_from_field")->where("id='".$form['id']."'")->update($modedata);
            }
        }
    }

    public function set_from_table($data)
    {
        $listdata=$data['set_from']['table'];
        foreach($listdata as $key2 =>$rn2) {
            unset($rn2['id']);
            $form=Db::name("rn_curd_table")->where("c_app_num='".$rn2['c_app_num']."' and c_mode_num='".$rn2['c_mode_num']."' and rn_num='".$rn2['rn_num']."'")->field("id")->find();
            $modedata=array_merge($this->moren_data(),$rn2);
            $modedata['c_app_id']=$data['c_app_id'];
            $modedata['c_mode_id']=$data['c_mode_id'];
            $modedata['source_id'] = $data['c_mode_id'];
            if (empty($form['id']))
            {
                Db::name("rn_curd_table")->insertGetId($modedata);
            }
            else
            {
                Db::name("rn_curd_table")->where("id='".$form['id']."'")->update($modedata);
            }
        }
    }

    public function moren_data()
    {
        $rndata['rn_edit_time']=date("Y-m-d H:i:s");
        $rndata['rn_edit_name']=GetToken('adminname') ? : NULL;
        $rndata['rn_edit_name_id']=GetToken('adminid') ? : NULL;
        $rndata['rn_add_time']=date("Y-m-d H:i:s");
        $rndata['rn_add_name']=GetToken('adminname') ? : NULL;
        $rndata['rn_add_name_id']=GetToken('adminid') ? : NULL;
        $rndata['rn_del_time']=NULL;
        $rndata['rn_del_name']=NULL;
        $rndata['rn_del_name_id']=NULL;
        $rndata['rn_del']=0;
        return $rndata;
    }
}
